<?php if(!defined('SIDELOADED')) { echo 'Direct access is not allowed.'; }
/**
 * @author Tyil <info@tyil.nl>
 * @version 1.0 beta [30-03-2012]
 */

if(strpos(@$_SERVER['QUERY_STRING'], $config['router']['extension']) !== FALSE) {

  // Create an array to differentiate between the requested file and the variables
  $queryArray = explode($config['router']['extension'], $_SERVER['QUERY_STRING']);
  
  if(file_exists(PATH . '/content/pages/' . $queryArray[0] . '.php')) {
    // Only do this stuff if the requested file actually exists
    
    $queryVars = explode($config['router']['delimiter'], $queryArray[1]);

    // Start an output buffer
    ob_start();

    // Make the variables available inside the buffer
    $i = 1;

    foreach($queryVars as $queryVar) {

      if($queryVar != '') {
        // If the var is empty, it can be skipped

        if(strpos($queryVar, $config['router']['assignment']) !== FALSE) {

          $arr = explode($config['router']['assignment'], $queryVar, 2);
          
          $$arr[0] = $arr[1];

        } else {

          $$i = $queryVar;

        }

        $i++;

      }

    }

    require PATH . '/content/pages/' . $queryArray[0] . '.php';

    echo ob_get_clean();
  
  } else {
    // 404 - File not found
    
    ob_start();
    
    $request = $queryArray[0];
    
    require PATH . '/content/pages/' . $config['router']['404'] . '.php';
    
    echo ob_get_clean();
    
  }
  
} else {
  
  header('location:' . URL . '/' . $config['router']['default'] . $config['router']['extension'] . (strlen($_SERVER['QUERY_STRING']) > 0 ? '/' . $_SERVER['QUERY_STRING'] : null));
  
}
